﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MacomberMapSystem.Common.User_Interfaces.Editors
{
    /// <summary>
    /// This class provides an HTML editor control. 
    /// Thanks to Oscar Londono, at http://www.codeproject.com/Articles/12172/An-extended-RichTextBox-to-save-and-load-quot-HTML
    /// </summary>
    public partial class MM_HTML_Editor : UserControl
    {
        public MM_HTML_Editor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Update the HTML of our control based on input
        /// </summary>
        public String HTML
        {
            get { return rtbEditor.GetHTML(true, true); }
            set { rtbEditor.Rtf = ""; rtbEditor.AddHTML(value); }
        }

        /// <summary>
        /// Report whether the selection is hidden.
        /// </summary>
        public bool HideSelection
        {
            get { return rtbEditor.HideSelection; }
            set { rtbEditor.HideSelection = value; }
        }

        #region HTML Editor portion
        void rtbEditor_SelectionChanged(object sender, System.EventArgs e)
        {

            if (!rtbEditor.InternalUpdating)
                UpdateToolbar(); //Update the toolbar buttons
        }

        /// <summary>
        ///     Update the toolbar button statuses
        /// </summary>
        public void UpdateToolbar()
        {
            //This is done incase 2 different fonts are selected at the same time
            //If that is the case there is no selection font so I use the default
            //font instead.
            Font fnt;

            if (rtbEditor.SelectionFont != null)
                fnt = rtbEditor.SelectionFont;
            else
                fnt = rtbEditor.Font;

            //Do all the toolbar button checks
            tbbBold.Pushed = fnt.Bold; //bold button
            tbbItalic.Pushed = fnt.Italic; //italic button
            tbbUnderline.Pushed = fnt.Underline; //underline button
            tbbStrikeout.Pushed = fnt.Strikeout; //strikeout button
            tbbLeft.Pushed = (rtbEditor.SelectionAlignment == HorizontalAlignment.Left); //justify left
            tbbCenter.Pushed = (rtbEditor.SelectionAlignment == HorizontalAlignment.Center); //justify center
            tbbRight.Pushed = (rtbEditor.SelectionAlignment == HorizontalAlignment.Right); //justify right

            tbbSuperScript.Pushed = rtbEditor.IsSuperScript();
            tbbSubScript.Pushed = rtbEditor.IsSubScript();
        }

        private void tb1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
        {
            //Switch based on the tooltip of the button pressed
            //OR: This could be changed to switch on the actual button pressed (e.g. e.Button and the case would be tbbBold)
            switch (e.Button.ToolTipText)
            {
                case "Bold":
                    if (rtbEditor.SelectionFont != null)
                        rtbEditor.SelectionFont = new Font(rtbEditor.SelectionFont, rtbEditor.SelectionFont.Style ^ FontStyle.Bold);
                    break;

                case "Italic":
                    if (rtbEditor.SelectionFont != null)
                        rtbEditor.SelectionFont = new Font(rtbEditor.SelectionFont, rtbEditor.SelectionFont.Style ^ FontStyle.Italic);
                    break;
                case "Underline":
                    if (rtbEditor.SelectionFont != null)
                        rtbEditor.SelectionFont = new Font(rtbEditor.SelectionFont, rtbEditor.SelectionFont.Style ^ FontStyle.Underline);
                    break;
                case "Strikeout":
                    if (rtbEditor.SelectionFont != null)
                        rtbEditor.SelectionFont = new Font(rtbEditor.SelectionFont, rtbEditor.SelectionFont.Style ^ FontStyle.Strikeout);
                    break;

                case "Super":
                    if (tbbSuperScript.Pushed)
                    {
                        rtbEditor.SetSuperScript(true);
                        rtbEditor.SetSubScript(false);
                    }
                    else
                    {
                        rtbEditor.SetSuperScript(false);
                    }
                    break;

                case "Sub":
                    {
                        if (tbbSubScript.Pushed)
                        {
                            rtbEditor.SetSubScript(true);
                            rtbEditor.SetSuperScript(false);
                        }
                        else
                        {
                            rtbEditor.SetSubScript(false);
                        }
                    }
                    break;
                case "Left":
                    {
                        //change horizontal alignment to left
                        rtbEditor.SelectionAlignment = HorizontalAlignment.Left;
                    }
                    break;

                case "Right":
                    {
                        //change horizontal alignment to right
                        rtbEditor.SelectionAlignment = HorizontalAlignment.Right;
                    }
                    break;

                case "Center":
                    {
                        //change horizontal alignment to center
                        rtbEditor.SelectionAlignment = HorizontalAlignment.Center;
                    }
                    break;

                case "Open":
                    {
                        try
                        {
                            if ((ofd1.ShowDialog() == DialogResult.OK) && (ofd1.FileName.Length > 0))
                            {
                                string strExt = System.IO.Path.GetExtension(ofd1.FileName).ToLower();

                                if (strExt == ".rtf")
                                {
                                    rtbEditor.LoadFile(ofd1.FileName, RichTextBoxStreamType.RichText);
                                }
                                else if (strExt == ".txt")
                                {
                                    rtbEditor.LoadFile(ofd1.FileName, RichTextBoxStreamType.PlainText);
                                }
                                else if ((strExt == ".htm") || (strExt == ".html"))
                                {
                                    // Read the HTML format
                                    StreamReader sr = File.OpenText(ofd1.FileName);
                                    string strHTML = sr.ReadToEnd();
                                    sr.Close();

                                    rtbEditor.AddHTML(strHTML);
                                }
                            }
                        }
                        catch
                        {
                            MessageBox.Show("There was an error loading the file: " + ofd1.FileName);
                        }
                    }
                    break;

                case "Save":
                    {
                        if ((sfd1.ShowDialog() == DialogResult.OK) && (sfd1.FileName.Length > 0))
                        {
                            string strExt = System.IO.Path.GetExtension(sfd1.FileName).ToLower();

                            if (strExt == ".rtf")
                            {
                                rtbEditor.SaveFile(sfd1.FileName);
                            }
                            else if (strExt == ".txt")
                            {
                                rtbEditor.SaveFile(sfd1.FileName, RichTextBoxStreamType.PlainText);
                            }
                            else if ((strExt == ".htm") || (strExt == ".html"))
                            {
                                try
                                {
                                    // save as HTML format
                                    string strText = rtbEditor.GetHTML(true, true);

                                    if (File.Exists(sfd1.FileName))
                                        File.Delete(sfd1.FileName);

                                    StreamWriter sr = File.CreateText(sfd1.FileName);
                                    sr.Write(strText);
                                    sr.Close();
                                }
                                catch
                                {
                                    MessageBox.Show("There was an error saving the file: " + sfd1.FileName);
                                }
                            }
                        }
                    }
                    break;

                case "Font":
                    {
                        if (rtbEditor.SelectionFont != null)
                            fontDialog1.Font = rtbEditor.SelectionFont;
                        else
                            fontDialog1.Font = rtbEditor.Font;

                        if (fontDialog1.ShowDialog() == DialogResult.OK)
                        {
                            if (rtbEditor.SelectionFont != null)
                                rtbEditor.SelectionFont = fontDialog1.Font;
                            else
                                rtbEditor.Font = fontDialog1.Font;
                        }
                    }
                    break;

                case "Color":
                    {
                        colorDialog1.Color = rtbEditor.SelectionColor;

                        if (colorDialog1.ShowDialog() == DialogResult.OK)
                        {
                            rtbEditor.SelectionColor = colorDialog1.Color;
                        }
                    }
                    break;
            }

            UpdateToolbar(); //Update the toolbar buttons
        }

        #endregion
    }
}
    